package com.punai.framework.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.punai.common.constant.Constants;
import com.punai.common.core.domain.entity.SysUser;
import com.punai.common.core.domain.model.LoginUser;
import com.punai.common.utils.SecurityUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class EntityMetaObjectInsertUpdateHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        if (metaObject.hasSetter("createTime")) {
            Object createTime = this.getFieldValByName("createTime", metaObject);
            if (createTime == null) {
                this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
            }
        }
        if (metaObject.hasSetter("createBy")) {
            Object createTime = this.getFieldValByName("createBy", metaObject);
            if (createTime == null) {
                SysUser user = new SysUser();
                user.setUserName("未登录用户");
                user.setNickName("未登录用户");
                try{
                    LoginUser loginUser = SecurityUtils.getLoginUser();
                    user = loginUser.getUser();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                String username = user.getUserName();
                String nickName = user.getNickName();
                this.strictInsertFill(metaObject, "createBy",String.class,username +  Constants.NAME_SEPARATOR + nickName);
            }
        }
        if (metaObject.hasSetter("deptId")) {
            Object deptId = this.getFieldValByName("deptId", metaObject);
            if (deptId == null) {
                Long insertDeptId = -999L;
                try {
                    insertDeptId = SecurityUtils.getDeptId() == null ? -999L : SecurityUtils.getDeptId();
                }catch (Exception e) {
                    e.printStackTrace();
                }
                this.strictInsertFill(metaObject, "deptId",Long.class,insertDeptId);
            }
        }
        if (metaObject.hasSetter("userId")) {
            Object userId = this.getFieldValByName("userId", metaObject);
            if (userId == null) {
                Long insertUserId = -999L;
                try {
                    insertUserId = SecurityUtils.getUserId();
                }catch (Exception e) {
                    e.printStackTrace();
                }
                this.strictInsertFill(metaObject, "userId",Long.class, insertUserId);
            }
        }

        if (metaObject.hasSetter("updateTime")) {
            Object updateTime = this.getFieldValByName("updateTime", metaObject);
            if (updateTime == null) {
                this.strictInsertFill(metaObject, "updateTime", Date.class, new Date());
            }
        }
        if (metaObject.hasSetter("updateBy")) {
            Object updateBy = this.getFieldValByName("updateBy", metaObject);
            if (updateBy == null) {
                SysUser user = new SysUser();
                user.setUserName("未登录用户");
                user.setNickName("未登录用户");
                try{
                    LoginUser loginUser = SecurityUtils.getLoginUser();
                    user = loginUser.getUser();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                String username = user.getUserName();
                String nickName = user.getNickName();
                this.strictInsertFill(metaObject, "updateBy",String.class,username +  Constants.NAME_SEPARATOR + nickName);
            }
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        if (metaObject.hasSetter("updateTime")) {
            metaObject.setValue("updateTime",new Date());
            //this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date());
        }
        if (metaObject.hasSetter("updateBy")) {
            SysUser user = new SysUser();
            user.setUserName("未登录用户");
            user.setNickName("未登录用户");
            try{
                LoginUser loginUser = SecurityUtils.getLoginUser();
                user = loginUser.getUser();
            } catch (Exception e) {
                e.printStackTrace();
            }
            String username = user.getUserName();
            String nickName = user.getNickName();
            metaObject.setValue("updateBy",username +  Constants.NAME_SEPARATOR + nickName);
            //this.strictUpdateFill(metaObject, "updateBy",String.class,username +  Constants.NAME_SEPARATOR + nickName);
        }
    }
}
